const buildConfig = require('../../seeyonBuild');
console.log(buildConfig, 'pc');

// 用于Pc依赖注入v5的外置文件
var version = `?buildversion=${new Date().getTime()}`;
var smpOption = {
    script: `
        var _mine_getUrlSurffix = '';
        try {
            _mine_getUrlSurffix = window.top.CsrfGuard.getUrlSurffix() || '';
        } catch(e) {}
        var cssArr = ['${buildConfig.pathResolve.staticPath}/common/cap4/template/base/cap4-icon/iconfont.css${buildConfig.pathResolve.staticSuffix}'];
        var jsArr = [];

        function appendPath(type, param) {
            switch (type) {
                case 'css':
                    if (Array.isArray(param)) {
                        param.forEach(function(v) {
                            var cssFile = document.createElement('link');
                            cssFile.rel = 'stylesheet';
                            cssFile.href = v;
                            document.getElementsByTagName('head')[0].appendChild(cssFile);
                        });
                    }
                    break;
                case 'js':
                    if (Array.isArray(param)) {
                        param.forEach(function(v) {
                            var script = document.createElement('script');
                            script.type = 'text/javascript';
                            script.async = false;
                            script.src = v;
                            document.getElementsByTagName('head')[0].appendChild(script);
                        });
                    }
                    break;
                default:
            }
        }

        appendPath('css', cssArr);
        appendPath('js', jsArr);
    `,
    js: [
		`./static/js/polyfill.js${buildConfig.pathResolve.staticSuffix}`,
        `${process.env.NODE_ENV == 'development' ? '/seeyon/rest/cap4/form/headerJs' : '../../../../../../../rest/cap4/form/headerJs'}`,
        `${buildConfig.pathResolve.staticPath}/common/all-min.js${buildConfig.pathResolve.staticSuffix}`,
        `${buildConfig.pathResolve.staticPath}/common/cap4/common/i18n.js${buildConfig.pathResolve.staticSuffix}`,
        `${buildConfig.pathResolve.staticPath}/common/js/misc/jsonGateway-debug.js${buildConfig.pathResolve.staticSuffix}`,
        `${buildConfig.pathResolve.staticPath}//common/js/selectOrgSdk.js${buildConfig.pathResolve.staticSuffix}`,
    ],
    css: [
        `${buildConfig.pathResolve.staticPath}/common/all-min.css${buildConfig.pathResolve.staticSuffix}`,
        `${buildConfig.pathResolve.staticPath}/skin/dist/components/components_layout.css${buildConfig.pathResolve.staticSuffix}`,
        `${buildConfig.pathResolve.staticPath}/skin/dist/components/components_theme_default.css${buildConfig.pathResolve.staticSuffix}`
    ]
}

function V5Plugin() {
    // 本地启动，自动添加一些外部依赖。如：$
    this.v5options = smpOption;
}

V5Plugin.prototype.apply = function (compiler) {

    var v5options = this.v5options;
    compiler.plugin('compilation', function (compilation) {
        compilation.plugin('html-webpack-plugin-before-html-generation', function (htmlPluginData, callback) {
            htmlPluginData.plugin.options.v5options = v5options;
        });
    });
};

module.exports = V5Plugin;
